package com.endoc.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Process adb = null;
    private OutputStream os;
    private LinearLayoutCompat llAll;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         findViewById(R.id.bt_start).setOnClickListener(this);
         findViewById(R.id.bt_end).setOnClickListener(this);
       /*  button=new Button(this);
        button.setText("按钮");
        button.setHeight(100);
        button.setWidth(100);

        button.setBackgroundColor(getColor(R.color.colorPrimary));
        llAll = (LinearLayoutCompat) findViewById(R.id.ll_all);
        llAll.setBackgroundColor(getColor(R.color.colorAccent));
        llAll.addView(button);
        button.setScaleX(3);*/

        //execAdb("Set the maximum recording time, in seconds.  Default / maximum is 180000");

        //execAdb("screenrecord  --time-limit 20 /sdcard/demo.mp4");

        //execAdb(" screenrecord  --time-limit 10 ./demo.mp4");
    }

    /** execAdb， 执行adb命令 */
    private String execAdb(String command) {

     /*   try {
            Thread.sleep(3000); // 延迟录制
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }*/

        try {
            adb = Runtime.getRuntime().exec("su", null, null);
            os = adb.getOutputStream();
            // 这个command即关键所在
            os.write(("/system/bin/" + command).getBytes("ASCII"));
            os.flush();
            os.close();
            int res = adb.waitFor();  // 等待进程结束。0为进程正常录制完成
            return "" + res;          // adb进程结束后执行
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
           if(adb!=null){
            adb.destroy();
           }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_start:
                //Log.d("whtest","height=="+ button.getHeight()+" weight=="+button.getWidth());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        execAdb("screenrecord  --time-limit 180 /sdcard/demo.mp4");
                        //execRootCmd("tinycap **.wav");
                    }
                }).start();
                break;
            case R.id.bt_end:
                //if(adb!=null&&os!=null){
               /*     try {
                        os.write(("pkill -INT screenrecord").getBytes("ASCII"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/
                    //execAdb("pkill -INT screenrecord");
                    execRootCmd("pkill -INT screenrecord");
                //}
                break;
            default:
                break;
        }
    }

    public static String execRootCmd(String cmd) {
        String result = "";
        DataOutputStream dos = null;
        DataInputStream dis = null;

        try {
            Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
            dos = new DataOutputStream(p.getOutputStream());
            dis = new DataInputStream(p.getInputStream());

            //Log.i(TAG, cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            String line = null;
            while ((line = dis.readLine()) != null) {
                //Log.d("result", line);
                result += line;
            }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
